from exts import db
from werkzeug.security import generate_password_hash, check_password_hash


# 数据库表的定义
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    password = db.Column(db.String(50), nullable=False)
    nickname = db.Column(db.String(50), nullable=False)
    is_root = db.Column(db.Integer, default=0)

    def __repr__(self):
        return '<user %d %s %s %d>' % (self.id, self.password, self.nickname, self.isroot)

    def set_password(self, password):
        self.password = generate_password_hash(password)

    def validate_password(self, password):
        return check_password_hash(self.password, password)


class Singer(db.Model):
    singer_name = db.Column(db.String(50), primary_key=True)
    singer_name_e = db.Column(db.String(50))
    pic_src = db.Column(db.String(200))
    vocal_part = db.Column(db.String(20))  # 声部
    simple_intro = db.Column(db.Text)
    nation = db.Column(db.String(50))
    intro_list = db.Column(db.Text)
    exp_list = db.Column(db.Text)
    singer_to_characters = db.relationship('SingerToCharacter', backref='singer', lazy=True)


class Composer(db.Model):
    cps_name = db.Column(db.String(50), primary_key=True)
    cps_name_e = db.Column(db.String(50))
    pic_src = db.Column(db.String(200))
    intro = db.Column(db.Text)
    nation = db.Column(db.String(50))
    exp_list = db.Column(db.Text)
    operas = db.relationship('Opera', backref='composer', lazy=True)


class Opera(db.Model):
    opr_name = db.Column(db.String(50), primary_key=True)
    opr_name_e = db.Column(db.String(50))
    lang = db.Column(db.String(20))
    pic_src = db.Column(db.String(200))
    like_cnt = db.Column(db.Integer)
    simple_intro = db.Column(db.String(50))
    cps_name = db.Column(db.String(50), db.ForeignKey('composer.cps_name'))
    intro = db.Column(db.Text)
    songs = db.relationship('Song', backref='opera', lazy=True)
    characters = db.relationship('Character', backref='opera', lazy=True)
    plots = db.relationship('OperaPlot', backref='opera', lazy=True)


class Song(db.Model):
    name = db.Column(db.String(200), primary_key=True)
    name_e = db.Column(db.String(100))
    opr_name = db.Column(db.String(50), db.ForeignKey('opera.opr_name'), primary_key=True)
    mp3_src = db.Column(db.String(200))  # mp3路径
    score_src = db.Column(db.String(200))  # 歌曲路径
    like_cnt = db.Column(db.Integer)
    is_classic = db.Column(db.Boolean)
    act = db.Column(db.String(20))


class Character(db.Model):
    opr_name = db.Column(db.String(50), db.ForeignKey('opera.opr_name'))
    character_name = db.Column(db.String(50), primary_key=True)
    intro = db.Column(db.Text)
    pic_src = db.Column(db.String(200))
    singer_to_characters = db.relationship('SingerToCharacter', backref='character', lazy=True)


class OperaPlot(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    opr_name = db.Column(db.String(50), db.ForeignKey('opera.opr_name'))
    name = db.Column(db.String(50))
    plot = db.Column(db.Text)


class SingerToCharacter(db.Model):
    character_name = db.Column(db.String(50), db.ForeignKey('character.character_name'), primary_key=True)
    singer_name = db.Column(db.String(50), db.ForeignKey('singer.singer_name'), primary_key=True)
